package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizOrder;
import com.member.shop.domain.SysBizOrderService;
import com.member.shop.domain.SysBizUser;
import com.member.shop.mapper.SysBizOrderMapper;
import com.member.shop.mapper.SysBizOrderServiceMapper;
import com.member.shop.mapper.SysBizUserMapper;
import com.member.shop.service.IAlipayService;
import com.member.shop.service.IPayService;
import com.member.shop.service.ISysBizOrderServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 订单售后Service业务层处理
 *
 * @author luoyun
 * @date 2025-01-24
 */
@Slf4j
@Service
public class SysBizOrderServiceServiceImpl extends ServiceImpl<SysBizOrderServiceMapper, SysBizOrderService> implements ISysBizOrderServiceService
{
    @Resource
    private SysBizOrderServiceMapper sysBizOrderServiceMapper;
    @Resource
    private SysBizOrderMapper sysBizOrderMapper;
    @Resource
    private SysBizUserMapper sysBizUserMapper;
    @Resource
    private IAlipayService alipayService;
    @Resource
    private IPayService payService;

    /**
     * 查询订单售后
     *
     * @param id 订单售后主键
     * @return 订单售后
     */
    @Override
    public SysBizOrderService selectSysBizOrderServiceById(Long id)
    {
        return sysBizOrderServiceMapper.selectById(id);
    }


    @Override
    public List<SysBizOrderService> selectSysBizOrderServiceList(SysBizOrderService sysBizOrderService) {
        return sysBizOrderServiceMapper.selectSysBizOrderServiceList(sysBizOrderService);
    }

    @Override
    public int statusUpdate(SysBizOrderService sysBizOrderService) {
        SysBizOrder bizOrder = sysBizOrderMapper.selectById(sysBizOrderService.getOrderId());
        if(bizOrder != null){
            // auditStatus 审核状态  0 未审核 1 审核通过 2 审核不通过
            if(sysBizOrderService.getAuditStatus() == 1){
                bizOrder.setStatus(85);

                // 进行退款
                // 根据支付方式进行退款操作 (0：微信  1：支付宝  2：余额)
                Integer paymentType = bizOrder.getPaymentType();
                if(paymentType == 0){
                    String refundId = "R" + System.currentTimeMillis();
                    Boolean refund = payService.refund2(refundId, bizOrder,sysBizOrderService.getRefundAmount());
                    log.info("---- 微信退款状态 statusUpdate ：refund:{}",refund);
                    if(refund){
                        sysBizOrderService.setRefundStatus(1L);
                        sysBizOrderService.setRefundTime(DateUtils.getNowDate());
                    }else{
                        sysBizOrderService.setRefundStatus(2L);
                    }

                }else if (paymentType == 1){
                    String zfbStr = alipayService.refund(bizOrder.getTenantId(), bizOrder.getOrderCode(), sysBizOrderService.getRefundAmount().doubleValue(), "订单取消退款",bizOrder.getTransactionId());
                    log.info("---- zfbStr状态 statusUpdate ：zfbStr:{}",zfbStr);
                    if(zfbStr.equals("订单退款成功")){
                        sysBizOrderService.setRefundStatus(1L);
                        sysBizOrderService.setRefundTime(DateUtils.getNowDate());
                    }else {
                        sysBizOrderService.setRefundStatus(2L);
                    }
                }else if (paymentType == 2){
                    SysBizUser sysBizUser = sysBizUserMapper.selectById(bizOrder.getUserId());
                    sysBizUser.setSurplus(sysBizUser.getSurplus().add(sysBizOrderService.getRefundAmount()));
                    int flag = sysBizUserMapper.updateById(sysBizUser);
                    if(flag > 0){
                        sysBizOrderService.setRefundStatus(1L);
                        sysBizOrderService.setRefundTime(DateUtils.getNowDate());
                    }else {
                        sysBizOrderService.setRefundStatus(2L);
                    }
                }

            }
            if(sysBizOrderService.getAuditStatus() == 2){
                bizOrder.setStatus(88);
            }
            sysBizOrderMapper.updateById(bizOrder);
        }


        sysBizOrderService.setUpdateTime(DateUtils.getNowDate());
        sysBizOrderService.setAuditTime(DateUtils.getNowDate());
        return sysBizOrderServiceMapper.updateById(sysBizOrderService);
    }

    /**
     * 查询订单售后列表
     *
     * @param queryWrapper 订单售后
     * @return 订单售后
     */
    @Override
    public List<SysBizOrderService> selectSysBizOrderServiceList(LambdaQueryWrapper<SysBizOrderService> queryWrapper)
    {
        return sysBizOrderServiceMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单售后
     *
     * @param sysBizOrderService 订单售后
     * @return 结果
     */
    @Override
    public int insertSysBizOrderService(SysBizOrderService sysBizOrderService)
    {
        sysBizOrderService.setCreateTime(DateUtils.getNowDate());
        return sysBizOrderServiceMapper.insert(sysBizOrderService);
    }

    /**
     * 修改订单售后
     *
     * @param sysBizOrderService 订单售后
     * @return 结果
     */
    @Override
    public int updateSysBizOrderService(SysBizOrderService sysBizOrderService)
    {
        sysBizOrderService.setUpdateTime(DateUtils.getNowDate());
        return sysBizOrderServiceMapper.updateById(sysBizOrderService);
    }

    /**
     * 批量删除订单售后
     *
     * @param ids 需要删除的订单售后主键
     * @return 结果
     */
    @Override
    public int deleteSysBizOrderServiceByIds(Long[] ids)
    {
        return sysBizOrderServiceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除订单售后信息
     *
     * @param id 订单售后主键
     * @return 结果
     */
    @Override
    public int deleteSysBizOrderServiceById(Long id)
    {
        return sysBizOrderServiceMapper.deleteById(id);
    }

    @Override
    public SysBizOrderService selectSysBizOrderServiceByRefundNo(String refundNo) {
        SysBizOrderService sysBizOrderService = new SysBizOrderService();

        sysBizOrderService.setRefundNo(refundNo);

        List<SysBizOrderService> sysBizOrderServices = sysBizOrderServiceMapper.selectSysBizOrderServiceList(sysBizOrderService);
        if (CollectionUtils.isEmpty(sysBizOrderServices)) {
            return null;
        }
        return sysBizOrderServices.get(0);
    }


}

